Menu
Bantuan:Templat Mencipta dan menyunting templatTemplates are created and edited in much the same way as any other page on WikiMedia: choose an appropriate name, navigate to that page, then click the Edit tab or create a new page as needed. The only real difference is that templates will normally be placed in the "Template:" namespace rather than the (unprefixed) article namespace. Anything that can be included on a normal page or article can be included on a template, including other templates (though as a practical matter templates will often have relatively small contents). Beyond that, templates have access to programming features - parameters, parser functions, and magic words - which allow for context dependent use,[2] and special tags that help control which information is transcluded and which is not.
When creating templates, it's best to follow a few simple principles:
Parser functions are the WikiMedia toolkit for simple branchings, calculations, text manipulations, and the like. As a group, they take one or more parameters and return a wikitext value based on the parameters. There are two basic forms for Parser functions (distinguished by whether a pound sign follows the opening brackets):
Parser functions are primarily used for the evaluation of parameters or magic words, in order to produce different results in different contexts.
Core parser functions generally deal with text manipulation and WikiMedia specific tasks.
Description | Text Entered | Result |
---|---|---|
Uppercasing text | {{uc: Heavens to BETSY! }} | HEAVENS TO BETSY! |
Lowercasing text | {{lc: Heavens to BETSY! }} | heavens to betsy! |
Getting a namespace name | {{NS: 1 }} | Perbincangan |
Getting a wikipedia URL | {{fullurl: pagename }} | //ms.wikipedia.org/wiki/Pagename |
The parserFunctions extension gives more more programming-oriented parser functions.
Description | Text Entered | Result |
---|---|---|
Testing between options | {{#ifeq: yes | yes | Hooray...! | Darn...! }} {{#ifeq: yes | no | Hooray...! | Darn...! }} | Hooray...! Darn...! |
Testing if a parameter is set | {{#if: {{{param|}}} | Hooray...! | Darn...! }} | Darn...! |
making a calculation | {{#expr: ( 27 * 46 / pi ) round 2 }} | 395.34 |
These functions are discussed in greater detail on their respective help pages.
Parameters are special codes that allow wikitext to be fed to a template; the template can be constructed to produce different outcomes depending on the values of the parameters it receives. Parameters may have names, or may be referenced by the position of values provided in the template tag (the order of the parameters in the template itself is irrelevant). Parameters in a template take the form {{{...}}}, where the tripled curled-braces surround the name of a named parameter or the number of a positional parameter. Values entered for parameters can be quite long (see Help:Long parameter demo), and if needed the transclusion of an entire page may be used as the value for a parameter.
To see how parameters work, consider this example. Assume there is a template called 'peoplepets' that produces some text about people and their pets. A template tag for this template might look like the following:
The contents of the template itself, on the page Template:Peoplepets, looks as follows:
{{{1}}} and {{{2}}} own a {{{4}}} [[{{{kind}}}]] named {{{3}}} who is {{{age}}} years old.Thus, the two template tags above will transclude the following text, respectively:
Things to note:
Default values can be specified for parameters using the vertical pipe character: |. The above example template could be rewritten as follows (in which 'friend' is the default value for positional parameter 2, the 4th positional parameter defaults to emptiness, and the 'kind' of the pet defaults to 'dog'):
{{{1}}} and {{{2|friend}}} own a {{{4|}}} [[{{{kind|dog}}}]] named {{{3}}} who is {{{age}}} years old.A template tag like the following: {{ peoplepets | Bill || age = 7 | Queenie }} will then produce the phrase Bill and friend own a dog named Queenie who is 7 years old.
Parameters that are not used in the template are ignored if they are provided by the template tag. This was intended to prevent transclusions from breaking if a template is changed in a way that removes a parameter; there's no need to go to every page a template is transcluded onto and change the tags. This has an added benefit, however, of making some template tags more readable, through spacing, or by adding comments. Using {{t3d}}:
Description | Text Entered | Result |
---|---|---|
Adding comments using template {{t3d}} | {{t3d |a|b|c| row 1 |d|e|f| row 2 |g|h|i| row 3}} | a b c d e f |
Since {{t3d}} does not use parameters {{{4}}}, {{{8}}}, or {{{12}}} the 'row #' values have no effect on the produced wikitext.
Dummy named parameters can be used in any template tag; simply choose an unused parameter name and add it into the tag as 'unusedname = value'. One special case of a named dummy parameter is to use an empty string as the parameter name. This acts like a named parameter in that it isn't counted when assessing the unnamed parameter positions, but it doesn't risk accidentally conflicting with an actual named parameter.
Description | Text Entered | Result | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
dummy variables with {{chess position}} | {{chess position|= comments go here 8 |rd|nd|bd|qd|kd|bd|nd|rd|= 7 |pd|pd|pd| | |pd|pd|pd|= 6 | | | | | | | | |= 5 | | | |pd|pd| | | |= standard opening 4 | |pl| | | | |pl| |= 3 | | | | | | | |bl|= odd move 2 |pl| |pl|pl|pl|pl| |pl|= 1 |rl|nl|bl|ql|kl| |nl|rl|= a b c d e f g h|30}} |
|
{{chess position}} uses the first 64 unnamed parameters to specify pieces; adding in unnumbered dummy parameters for comments would have required that the template be rewritten (or that the unnumbered parameters be specified explicitly using the '#=value' format).
Parameters do not get expanded when they are inside nowiki tags or XML-style extension tags. Thus, the following will not work within a template - <myextension xparam={{{tparam}}}> ... </myextension> - because the parameter is not expanded:
Because template tags, parser functions, magic words, and parameters are all defined using curled braces, there are times when their combined use can be ambiguous. For example, the construction {{{{{NAMESPACE}}}}} might be interpreted as:
In general, wherever the parser finds more than 3 opening braces in a row, it will assume it is a parameter if the matching closing braces are 3-in-a-row, or as a template tag if only two of the matching closing braces are contiguous. In some cases it is advisable to add a space in the opening brackets to clarify the construction. For instance, the {{{{{NAMESPACE}}}}} construction from above will always default to the first interpretation; it might be better to use {{{ {{NAMESPACE}}}}} or {{ {{{NAMESPACE}}}}} as required so that the intention of the code is clear to other editors.
Templates may contain other templates - usually called 'nesting'. As the template is processed, the wikitext produced by any nested templates is transcluded into the nesting template, so that the final product is essentially processed from the most deeply nested template out. While fairly straight-forward in application, it involves some noteworthy quirks and tricks.
To pass a parameter value to a nested template, place a parameter tag as the value of one of the nested template's parameters.
Template parameters themselves can be chosen conditionally.
The Wikimedia software will allow a template to call itself, but will stop after one iteration to prevent an infinite loop.
When a nested template contains unmatched curled-brackets - as in {{lb}} - the unmatched brackets are treated as text during processing, and do not affect the parsing of brackets in the nesting template. If the nested template is substituted, however, the substitution is processed first, and this will change how braces are parsed in the nesting template. This has little practical use, but can occasionally introduce unexpected errors.
See the help:advanced templates and meta:Help:Recursive conversion of wikitext for more information.
Several tags are available to control what does and doesn't get transcluded from a template.[3] The three tags are noinclude, includeonly, and onlyinclude. They are used in the same fashion as standard HTML or XML blocks: opening and closing tags in angled brackets (e.g. <noinclude>affected wikitext</noinclude>).
Perhaps the most common issue with the use of these blocks is unwanted spaces or lines. It is important to remember that the effect of these tags ends immediately after the last angle bracket, not on the next line or with the next visible character.
These tags can be nested inside each other, though (for a given page) this really only applies to the 'onlyinclude' tag; nesting 'includeonly' and 'noinclude' tags is fairly pointless. Be careful not to split the tags, however. Constructions like this <onlyinclude>abc<includeonly>def</onlyinclude>ghi</includeonly> will not work as expected. Use the 'first opened, last closed' rule that is standard for XML.
Note further that expansions may not occur the way expected. For instance, ~<includeonly>~</includeonly>~~ will be displayed as ~~~ in the template, and will be transcluded as ~~~~, but it will not be expanded in either case (as user name or the name/date combination). The template would have to be substituted to get this wikitext to expand.
The 'noinclude' tag is used to prevent text on the template page from being transcluded onto other pages. This is often used for:
Its use is straightforward:
If this text were in a template page, this part would be transcluded<noinclude>but this text would not be transcluded</noinclude>One of the peculiarities of 'noinclude' tags is that the text contained in them is actually processed, even though it is not included in the rendered output. This has to do with error reporting: the processor needs to retain the text while it checks that there is a valid </noinclude> closing tag, and this consumes system resources. For templates that are not heavily used, this is rarely a problem, but on some templates excess text (and interwiki links in particular) can create a tremendous overhead for the system. As a rule, interwikis and documentation should be placed on a /doc subpage (such as is generated automatically by the {{Documentation}} template).
The 'includeonly' tag is the opposite of the 'noinclude' tag. Text within an include only block is only included when the page is transcluded, is does not appear on the template page itself. This is frequently used for:
Its use is straightforward:
If this text were in a template page, this part would appear on the template page and on the transclusion page<includeonly>but this text would only appear on the transclusion page</includeonly>The 'onlyinclude' tag literally only includes the material that is between the tags; anything else on the page - even text within 'includeonly' tags, will appear on the template page but will not be included. This is not frequently used, but might be useful when only a small bit of text in the middle of an otherwise large page is to be transcluded.It is used like this:
If this text were in a template page, this part would be visible there, but would not be transcluded<includeonly>This text would not be visible on the template page, nor would it be transcluded</includeonly><onlyinclude>this text is all that would be transcluded</onlyinclude>There are three general types of magic words on Wikimedia pages. One - parser functions - are already discussed above. The second type - behavioral switches - involves page structure modifications which are of little use on templates. The last type, however - variables (or system variables) - are often used on templates. These magic words provide information directly from the system itself: local dates and times, information about the current articles or pages, and even information about the wiki itself.
System variables take the format {{...}} where the enclosed text is always entirely capitalized. A small number of system variables take a parameter as well, using the standard colon separator - {{MAGICWORD:parameter}}. If the enclosed text starts and ends with doubled underscores (__...__) it is a behavioral switch, not a variable.
Description | Text Entered | Result (for this help page) |
---|---|---|
Page names | {{PAGENAME}} {{FULLPAGENAME}} | Templat Bantuan:Templat |
Name of the current namespace | {{NAMESPACE}} | Bantuan |
Number of registered users | {{NUMBEROFUSERS}} | 248,989 |
Number of pages in a given category | {{PAGESINCATEGORY:"Weird Al" Yankovic albums}} | |
Current software version | {{CURRENTVERSION}} | 1.35.0-wmf.15 (5baae39) |
Timestamp of last revision | {{REVISIONTIMESTAMP}} | 20190301111046 |
The PAGENAME and NAMESPACE variables are particularly useful, and frequently used, to change template behavior based on context. For example, if the template transcludes a category link (e.g. cleanup templates, which transclude a link categorizing the page as a page which needs cleanup), it will often check the NAMESPACE variable to make sure that talk pages, user pages, or anywhere else the tag might incidentally be placed do not themselves get categorized as pages needing cleanup.
Transclusion {{msgnw demo}} | Substitution {{subst:msgnw demo}} | Message, no wiki {{msgnw:msgnw demo}} | |
---|---|---|---|
Templat:Msgnw demo |
jklmno | [[:Templat:Msgnw demo]] |
Click the edit link for this section to see how the substituted section is different.
Menu
Bantuan:Templat Mencipta dan menyunting templatBerkaitan
Bantuan pengangguran Bantuan Bantuan kemanusiaan Bantuan brek kecemasan Bantuan Rakyat 1Malaysia Bantuan Motor Bersepadu Bantuan pembangunan rasmi Bantul Bantul, Bantul Bantuil, Cerbon, Barito KualaRujukan
WikiPedia: Bantuan:Templat http://translate.google.com/translate?u=https://en... http://prpm.dbp.gov.my/ http://meta.wikimedia.org//w/index.php?title=Bantu... http://meta.wikimedia.org//w/index.php?title=Bantu... http://en.wikipedia.org/w/index.php?title=Template... https://meta.wikimedia.org/wiki/Help:Template?oldi... https://meta.wikimedia.org/wiki/Template:Loop1?act...